package com.eduhzy.ttw.teacher.mvp.presenter;

import android.app.Application;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.entity.MineClassData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.teacher.mvp.contract.MineClassContract;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


@ActivityScope
public class MineClassPresenter extends BasePresenter<MineClassContract.Model, MineClassContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    @Inject
    BaseQuickAdapter<MineClassData, AutoBaseViewHolder> mAdapter;

    @Inject
    public MineClassPresenter(MineClassContract.Model model, MineClassContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void getClassByUser() {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("userId", SPUtils.getInstance().getString(Constants.USERID));
        mModel.getClassByUser(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();//显示下拉刷新的进度条
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();//隐藏下拉刷新的进度条
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<CommonData<List<MineClassData>>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<List<MineClassData>> data) {
                        mAdapter.getData().clear();
                        if (data.isResult()) {
                            List<MineClassData> list = data.getData();
                            mAdapter.addData(list);
                        }
                        if (mAdapter.getEmptyViewCount() <= 0) {
                            mRootView.showAdapterEmpty();
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        if (mAdapter.getEmptyViewCount() <= 0) {
                            mRootView.showAdapterEmpty();
                        }
                    }
                });
    }

    public void deleteClass(String id, String string, int position) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("id", id);
        map.put("teacherId", string);
        mModel.deleteClass(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 3))
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<Map>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<Map> commonData) {
                        if (commonData.isResult()) {
                            try {
                                mRootView.showMessage("删除成功！");
                                mAdapter.remove(position);
                                if (mAdapter.getData().size() > position) {
                                    mAdapter.notifyItemChanged(position);
                                } else {
                                    mAdapter.notifyDataSetChanged();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }
}
